/*
 * @Author: 晟松
 * @Date: 2021-11-30 13:11:23
 * @LastEditors: 晟松
 * @LastEditTime: 2022-02-12 21:59:43
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\src\main\controller\UserController.js
 * @Description:
 */
// src/main/controller/UserController.js
import Router from "koa-router";
import UserService from "@main/service/UserService";
import koaBodyparser from "koa-bodyparser";
import { isNull, isBlank } from "@main/util/ValidateUtil";
import { ParamError } from "@main/common/CommonError";

let userRouter = new Router({
    prefix: "/api/user",
});

// 算了，post获得用户信息得了
// userRouter.get("/:id", async (ctx) => {
//     console.log("ctx.request", ctx.params, ctx.query.identity);
//     ctx.body = ctx.renderJson(await UserService.findById(ctx.params.id));
// });

userRouter.post("/info", koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await UserService.findUserInfo(params));
});
/**
 * @description: 获得自己的用户信息，通过jwt的payload。注意必须是post请求才行
 * @param {*}
 * @return {*}
 */
userRouter.post("/mine", async (ctx) => {
    // console.log("ctx.request", ctx.params, ctx.query.identity);
    let user = ctx.state.user;
    // console.log('user: ', user);
    ctx.body = ctx.renderJson(user);
});

userRouter.post("/create", koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    // 这里可以以后会对校级管理员和居委会的注册进行限制，只会允许学生和居民和院级管理员进行注册

    ctx.body = ctx.renderJson(await UserService.create(params));
});

/**
 * @description: 更新用户信息
 * @param {*}
 * @return {*}
 */
// 试一下RESTFUL API的接口方式
userRouter.put("/", koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await UserService.updateById(params));
});
/**
 * @description: 更新用户密码
 * @param {*}
 * @return {*}
 */
userRouter.post("/password", koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await UserService.updatePassword(params));
});
/**
 * @description: 删除用户信息
 * @param {*}
 * @return {*}
 */
userRouter.delete("/", koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await UserService.deleteById(params));
});

userRouter.post("/updatePwd", koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await UserService.updatePwd(params));
});

userRouter.get("/getAllCommunity", koaBodyparser(), async (ctx) => {
    ctx.body = ctx.renderJson(await UserService.getAllCommunity());
});

userRouter.get("/getAllSchool", koaBodyparser(), async (ctx) => {
    ctx.body = ctx.renderJson(await UserService.getAllSchool());
});

userRouter.get("/getAllCommunityInfo", koaBodyparser(), async (ctx) => {
    ctx.body = ctx.renderJson(await UserService.getAllCommunityInfo());
});

userRouter.get("/getAllSchoolInfo", koaBodyparser(), async (ctx) => {
    ctx.body = ctx.renderJson(await UserService.getAllSchoolInfo());
});
/**
 * @description: 获得所有学生和居民的积分排行
 * @param {*}
 * @return {*}
 */
userRouter.get("/getIntegralRank", koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    ctx.body = ctx.renderJson(await UserService.getIntegralRank(params));
});


export default userRouter;
